

%% drop top 5% '09-'12 spending increases %%

load('../../voting_stage.mat')
load('../../campaign_stage.mat')
load('../../coalition_stage_p1.mat')
load('../../coalition_stage.mat','theta_noCovs','theta')

dChar = readtable('../../../raw_data/district_characteristics.csv');
e09 = readtable('../../../raw_data/election_results_2009.csv');
e12 = readtable('../../../raw_data/election_results_2012.csv');
cs09 = readtable('../../../raw_data/campaign_spending_2009.csv');
cs12 = readtable('../../../raw_data/campaign_spending_2012.csv');
% order observations by candidate-coalition choice
d0 = find(e12.candCM==0); N0 = size(d0,1);
d1 = find(e12.candCM==1); N1 = size(d1,1);
d2 = find(e12.candCM==2); N2 = size(d2,1);
cs09 = cs09([d0;d1;d2],:); cs12 = cs12([d0;d1;d2],:);
dChar = dChar([d0;d1;d2],:);
e12 = e12([d0;d1;d2],:);
clear d0 d1 d2
% find largest spending discrepancies
sd = (cs12.PRI + cs12.PVEM + cs12.CM - ...
    (cs09.PRI + cs09.PVEM + cs09.PM)) ./ (cs09.PRI + cs09.PVEM + cs09.PM) <= ...
    prctile((cs12.PRI + cs12.PVEM + cs12.CM - ...
    (cs09.PRI + cs09.PVEM + cs09.PM)) ./ (cs09.PRI + cs09.PVEM + cs09.PM),95);
sd_long = [sd;sd;sd(1:N0);sd(N0+N1+(1:N2));sd(1:N0);sd(N0+(1:N1));sd];
dChar = dChar(sd,:);
e12 = e12(sd,:);
D = D(sd,:);
PW0_PVEM = PW0_PVEM(sd,:);
PW0_PRI = PW0_PRI(sd,:);
DPW0_PVEM = DPW0_PVEM(sd,:);
DPW0_PRI = DPW0_PRI(sd,:);
ES0 = ES0(sd,:);
ES1 = ES1(sd,:);
ES2 = ES2(sd,:);
DES0 = DES0(sd,:);
DES1 = DES1(sd,:);
DES2 = DES2(sd,:);
Z = Z(sd_long,:);
C = C(sd_long,:);
br0 = br0(sd_long,:);
Z0 = Z0(sd_long,:);
xi0 = xi0(sd_long,:);
X2S0 = X2S0([sd(N0+1:end);sd(N0+1:end)],:);
xi2S0 = xi2S0([sd(N0+1:end);sd(N0+1:end)],:);
N0 = sum(e12.candCM==0);
N1 = sum(e12.candCM==1);
N2 = sum(e12.candCM==2); N = N0 + N1 + N2;
A = [1:N,1:N,1:N0,N0+N1+(1:N2),1:N0+N1,1:N]';
A = 1 * (A==A');


%% "entry" parameters: parsimonious

% no-candidate payoff parameters
X_PVEM = [ones(N,1),zeros(N,1),PW0_PVEM];
X_PRI = [zeros(N,1),ones(N,1),PW0_PRI];
optCFS = optimset('Display','off','HessFcn',@(thetaNC,lambda)HessLL_cfs(thetaNC,lambda,X_PVEM,X_PRI,ES0,ES1,ES2));
[theta_noCovs,~,exit_cfs_noCovs5] = knitro_nlp(@(thetaNC)LL_cfs(thetaNC,X_PVEM,X_PRI,ES0,ES1,ES2,[N0,N1,N2]),...
    theta_noCovs,[],[],[],[],[],[],[],[],optCFS,'knitro.opt');

% robust inference (accounting for voting- and campaign-stage estimation uncertainty)
% gradient of joint moment conditions
G_all = grad_cfs_moments(theta_noCovs,X_PVEM,X_PRI,ES0,ES1,ES2,DES0,DES1,DES2,DPW0_PVEM,DPW0_PRI);
G_all = [G_all;... % gradient of coalition-stage MLE FOCs
    zeros(size(Ghat,1),length(theta_noCovs)),Ghat,zeros(size(Ghat,1),length(B2S0));... % gradient of campaign- and voting-first-stage MCs
    zeros(size(X2S0,2),length(theta_noCovs)+length(gamma)+length(B)),X2S0'*X2S0]; % gradient of voting-second-stage MCs
% joint covariance matrix
% coalition-stage score
P1 = exp(X_PVEM * theta_noCovs + ES1 - ES0);
P2 = exp(X_PRI * theta_noCovs + ES2 - ES0);
P0 = 1 + P1 + P2;
P1 = P1 ./ P0;
P2 = P2 ./ P0;
L = (([zeros(N0,1);ones(N1,1);zeros(N2,1)] - P1) .* X_PVEM + ...
        ([zeros(N0+N1,1);ones(N2,1)] - P2) .* X_PRI)';
L = - L';
% campaign-stage MCs
Zc = (A(1:N,:) ./ sum(A(1:N,:),2)) * (Z .* (C - br0));
% voting-first-stage MCs
Zxi = (A(1:N,:) ./ sum(A(1:N,:),2)) * (Z0 .* xi0);
% voting-second-stage MCs
Zxi2S = [zeros(N0,size(X2S0,2));(repmat(A(N0+1:N,N0+1:N),1,2) / 2) * (X2S0 .* xi2S0)];
Omega_all = [L' * L,L' * Zc,L' * Zxi,L' * Zxi2S; ...
    Zc' * L,Zc' * Zc,Zc' * Zxi,Zc' * Zxi2S; ...
    Zxi' * L,Zxi' * Zc,Zxi' * Zxi,Zxi' * Zxi2S; ...
    Zxi2S' * L,Zxi2S' * Zc,Zxi2S' * Zxi,Zxi2S' * Zxi2S];
V_all_noCovs = inv(G_all' * (Omega_all \ G_all));
seTheta_noCovs = sqrt(diag(V_all_noCovs));
seTheta_noCovs = seTheta_noCovs(1:length(theta_noCovs));
rTheta_noCovs5 = [theta_noCovs,seTheta_noCovs,2*(1-normcdf(abs(theta_noCovs./seTheta_noCovs)))];

disp(' ')
disp('for Table D3 (column (III)):')
disp(print_results_theta(round(rTheta_noCovs5,3),{},{'constant'}))


%% "entry" parameters: rich

% no-candidate payoff parameters
X_PVEM = [dChar.region==1,dChar.region==2,dChar.region==3,dChar.region==4,dChar.region==5,D,zeros(N,5+KD),PW0_PVEM];
X_PRI = [zeros(N,5+KD),dChar.region==1,dChar.region==2,dChar.region==3,dChar.region==4,dChar.region==5,D,PW0_PRI];
optCFS = optimset('Display','off','HessFcn',@(thetaNC,lambda)HessLL_cfs(thetaNC,lambda,X_PVEM,X_PRI,ES0,ES1,ES2));
[theta,~,exit_cfs5] = knitro_nlp(@(thetaNC)LL_cfs(thetaNC,X_PVEM,X_PRI,ES0,ES1,ES2,[N0,N1,N2]),...
    theta,[],[],[],[],[],[],[],[],optCFS,'knitro.opt');

% robust inference (accounting for voting- and campaign-stage estimation uncertainty)
% gradient of moment conditions
G_all = grad_cfs_moments(theta,X_PVEM,X_PRI,ES0,ES1,ES2,DES0,DES1,DES2,DPW0_PVEM,DPW0_PRI);
G_all = [G_all;... % gradient of coalition-stage MLE FOCs
    zeros(size(Ghat,1),length(theta)),Ghat,zeros(size(Ghat,1),length(B2S0));... % gradient of campaign- and voting-first-stage MCs
    zeros(size(X2S0,2),length(theta)+length(gamma)+length(B)),X2S0'*X2S0]; % gradient of voting-second-stage MCs
% joint covariance matrix
% coalition-stage score
P1 = exp(X_PVEM * theta + ES1 - ES0);
P2 = exp(X_PRI * theta + ES2 - ES0);
P0 = 1 + P1 + P2;
P1 = P1 ./ P0;
P2 = P2 ./ P0;
L = (([zeros(N0,1);ones(N1,1);zeros(N2,1)] - P1) .* X_PVEM + ...
        ([zeros(N0+N1,1);ones(N2,1)] - P2) .* X_PRI)';
L = - L';
% campaign-stage MCs
Zc = (A(1:N,:) ./ sum(A(1:N,:),2)) * (Z .* (C - br0));
% voting-first-stage MCs
Zxi = (A(1:N,:) ./ sum(A(1:N,:),2)) * (Z0 .* xi0);
% voting-second-stage MCs
Zxi2S = [zeros(N0,size(X2S0,2));(repmat(A(N0+1:N,N0+1:N),1,2) / 2) * (X2S0 .* xi2S0)];
Omega_all = [L' * L,L' * Zc,L' * Zxi,L' * Zxi2S; ...
    Zc' * L,Zc' * Zc,Zc' * Zxi,Zc' * Zxi2S; ...
    Zxi' * L,Zxi' * Zc,Zxi' * Zxi,Zxi' * Zxi2S; ...
    Zxi2S' * L,Zxi2S' * Zc,Zxi2S' * Zxi,Zxi2S' * Zxi2S];
V_all = inv(G_all' * (Omega_all \ G_all));
seTheta = sqrt(diag(V_all));
seTheta = seTheta(1:length(theta));
rTheta5 = [theta,seTheta,2*(1-normcdf(abs(theta./seTheta)))];

disp(' ')
disp('for Table D3 (column (VII)):')
disp(print_results_theta(round(rTheta5,3),dnames,{'region1','region2','region3','region4','region5'}))


%% drop top 10% '09-'12 spending increases %%

load('../../voting_stage.mat')
load('../../campaign_stage.mat')
load('../../coalition_stage_p1.mat')
load('../../coalition_stage.mat','theta_noCovs','theta')

dChar = readtable('../../../raw_data/district_characteristics.csv');
e09 = readtable('../../../raw_data/election_results_2009.csv');
e12 = readtable('../../../raw_data/election_results_2012.csv');
cs09 = readtable('../../../raw_data/campaign_spending_2009.csv');
cs12 = readtable('../../../raw_data/campaign_spending_2012.csv');
% order observations by candidate-coalition choice
d0 = find(e12.candCM==0); N0 = size(d0,1);
d1 = find(e12.candCM==1); N1 = size(d1,1);
d2 = find(e12.candCM==2); N2 = size(d2,1);
cs09 = cs09([d0;d1;d2],:); cs12 = cs12([d0;d1;d2],:);
dChar = dChar([d0;d1;d2],:);
e12 = e12([d0;d1;d2],:);
clear d0 d1 d2
% find largest spending discrepancies
sd = (cs12.PRI + cs12.PVEM + cs12.CM - ...
    (cs09.PRI + cs09.PVEM + cs09.PM)) ./ (cs09.PRI + cs09.PVEM + cs09.PM) <= ...
    prctile((cs12.PRI + cs12.PVEM + cs12.CM - ...
    (cs09.PRI + cs09.PVEM + cs09.PM)) ./ (cs09.PRI + cs09.PVEM + cs09.PM),90);
sd_long = [sd;sd;sd(1:N0);sd(N0+N1+(1:N2));sd(1:N0);sd(N0+(1:N1));sd];
dChar = dChar(sd,:);
e12 = e12(sd,:);
D = D(sd,:);
PW0_PVEM = PW0_PVEM(sd,:);
PW0_PRI = PW0_PRI(sd,:);
DPW0_PVEM = DPW0_PVEM(sd,:);
DPW0_PRI = DPW0_PRI(sd,:);
ES0 = ES0(sd,:);
ES1 = ES1(sd,:);
ES2 = ES2(sd,:);
DES0 = DES0(sd,:);
DES1 = DES1(sd,:);
DES2 = DES2(sd,:);
Z = Z(sd_long,:);
C = C(sd_long,:);
br0 = br0(sd_long,:);
Z0 = Z0(sd_long,:);
xi0 = xi0(sd_long,:);
X2S0 = X2S0([sd(N0+1:end);sd(N0+1:end)],:);
xi2S0 = xi2S0([sd(N0+1:end);sd(N0+1:end)],:);
N0 = sum(e12.candCM==0);
N1 = sum(e12.candCM==1);
N2 = sum(e12.candCM==2); N = N0 + N1 + N2;
A = [1:N,1:N,1:N0,N0+N1+(1:N2),1:N0+N1,1:N]';
A = 1 * (A==A');


%% "entry" parameters: parsimonious

% no-candidate payoff parameters
X_PVEM = [ones(N,1),zeros(N,1),PW0_PVEM];
X_PRI = [zeros(N,1),ones(N,1),PW0_PRI];
optCFS = optimset('Display','off','HessFcn',@(thetaNC,lambda)HessLL_cfs(thetaNC,lambda,X_PVEM,X_PRI,ES0,ES1,ES2));
[theta_noCovs,~,exit_cfs_noCovs10] = knitro_nlp(@(thetaNC)LL_cfs(thetaNC,X_PVEM,X_PRI,ES0,ES1,ES2,[N0,N1,N2]),...
    theta_noCovs,[],[],[],[],[],[],[],[],optCFS,'knitro.opt');

% robust inference (accounting for voting- and campaign-stage estimation uncertainty)
% gradient of joint moment conditions
G_all = grad_cfs_moments(theta_noCovs,X_PVEM,X_PRI,ES0,ES1,ES2,DES0,DES1,DES2,DPW0_PVEM,DPW0_PRI);
G_all = [G_all;... % gradient of coalition-stage MLE FOCs
    zeros(size(Ghat,1),length(theta_noCovs)),Ghat,zeros(size(Ghat,1),length(B2S0));... % gradient of campaign- and voting-first-stage MCs
    zeros(size(X2S0,2),length(theta_noCovs)+length(gamma)+length(B)),X2S0'*X2S0]; % gradient of voting-second-stage MCs
% joint covariance matrix
% coalition-stage score
P1 = exp(X_PVEM * theta_noCovs + ES1 - ES0);
P2 = exp(X_PRI * theta_noCovs + ES2 - ES0);
P0 = 1 + P1 + P2;
P1 = P1 ./ P0;
P2 = P2 ./ P0;
L = (([zeros(N0,1);ones(N1,1);zeros(N2,1)] - P1) .* X_PVEM + ...
        ([zeros(N0+N1,1);ones(N2,1)] - P2) .* X_PRI)';
L = - L';
% campaign-stage MCs
Zc = (A(1:N,:) ./ sum(A(1:N,:),2)) * (Z .* (C - br0));
% voting-first-stage MCs
Zxi = (A(1:N,:) ./ sum(A(1:N,:),2)) * (Z0 .* xi0);
% voting-second-stage MCs
Zxi2S = [zeros(N0,size(X2S0,2));(repmat(A(N0+1:N,N0+1:N),1,2) / 2) * (X2S0 .* xi2S0)];
Omega_all = [L' * L,L' * Zc,L' * Zxi,L' * Zxi2S; ...
    Zc' * L,Zc' * Zc,Zc' * Zxi,Zc' * Zxi2S; ...
    Zxi' * L,Zxi' * Zc,Zxi' * Zxi,Zxi' * Zxi2S; ...
    Zxi2S' * L,Zxi2S' * Zc,Zxi2S' * Zxi,Zxi2S' * Zxi2S];
V_all_noCovs = inv(G_all' * (Omega_all \ G_all));
seTheta_noCovs = sqrt(diag(V_all_noCovs));
seTheta_noCovs = seTheta_noCovs(1:length(theta_noCovs));
rTheta_noCovs10 = [theta_noCovs,seTheta_noCovs,2*(1-normcdf(abs(theta_noCovs./seTheta_noCovs)))];

disp(' ')
disp('for Table D3 (column (IV)):')
disp(print_results_theta(round(rTheta_noCovs10,3),{},{'constant'}))


%% "entry" parameters: rich

% no-candidate payoff parameters
X_PVEM = [dChar.region==1,dChar.region==2,dChar.region==3,dChar.region==4,dChar.region==5,D,zeros(N,5+KD),PW0_PVEM];
X_PRI = [zeros(N,5+KD),dChar.region==1,dChar.region==2,dChar.region==3,dChar.region==4,dChar.region==5,D,PW0_PRI];
optCFS = optimset('Display','off','HessFcn',@(thetaNC,lambda)HessLL_cfs(thetaNC,lambda,X_PVEM,X_PRI,ES0,ES1,ES2));
[theta,~,exit_cfs10] = knitro_nlp(@(thetaNC)LL_cfs(thetaNC,X_PVEM,X_PRI,ES0,ES1,ES2,[N0,N1,N2]),...
    theta,[],[],[],[],[],[],[],[],optCFS,'knitro.opt');

% robust inference (accounting for voting- and campaign-stage estimation uncertainty)
% gradient of moment conditions
G_all = grad_cfs_moments(theta,X_PVEM,X_PRI,ES0,ES1,ES2,DES0,DES1,DES2,DPW0_PVEM,DPW0_PRI);
G_all = [G_all;... % gradient of coalition-stage MLE FOCs
    zeros(size(Ghat,1),length(theta)),Ghat,zeros(size(Ghat,1),length(B2S0));... % gradient of campaign- and voting-first-stage MCs
    zeros(size(X2S0,2),length(theta)+length(gamma)+length(B)),X2S0'*X2S0]; % gradient of voting-second-stage MCs
% joint covariance matrix
% coalition-stage score
P1 = exp(X_PVEM * theta + ES1 - ES0);
P2 = exp(X_PRI * theta + ES2 - ES0);
P0 = 1 + P1 + P2;
P1 = P1 ./ P0;
P2 = P2 ./ P0;
L = (([zeros(N0,1);ones(N1,1);zeros(N2,1)] - P1) .* X_PVEM + ...
        ([zeros(N0+N1,1);ones(N2,1)] - P2) .* X_PRI)';
L = - L';
% campaign-stage MCs
Zc = (A(1:N,:) ./ sum(A(1:N,:),2)) * (Z .* (C - br0));
% voting-first-stage MCs
Zxi = (A(1:N,:) ./ sum(A(1:N,:),2)) * (Z0 .* xi0);
% voting-second-stage MCs
Zxi2S = [zeros(N0,size(X2S0,2));(repmat(A(N0+1:N,N0+1:N),1,2) / 2) * (X2S0 .* xi2S0)];
Omega_all = [L' * L,L' * Zc,L' * Zxi,L' * Zxi2S; ...
    Zc' * L,Zc' * Zc,Zc' * Zxi,Zc' * Zxi2S; ...
    Zxi' * L,Zxi' * Zc,Zxi' * Zxi,Zxi' * Zxi2S; ...
    Zxi2S' * L,Zxi2S' * Zc,Zxi2S' * Zxi,Zxi2S' * Zxi2S];
V_all = inv(G_all' * (Omega_all \ G_all));
seTheta = sqrt(diag(V_all));
seTheta = seTheta(1:length(theta));
rTheta10 = [theta,seTheta,2*(1-normcdf(abs(theta./seTheta)))];

disp(' ')
disp('for Table D3 (column (VIII)):')
disp(print_results_theta(round(rTheta10,3),dnames,{'region1','region2','region3','region4','region5'}))


%%

clearvars -except exit_cfs_noCovs5 exit_cfs_noCovs10 ...
    rTheta_noCovs5 rTheta_noCovs10 ...
    exit_cfs5 rTheta5 exit_cfs10 rTheta10

save('coalition_stage_dropOutliers.mat')



